package com.genericdevice.platform.core.dialog.mvp;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import com.genericdevice.platform.core.dialog.helpers.FileListHelper;
import com.genericdevice.platform.core.event.DefaultEvent;
import com.genericdevice.platform.core.event.EventBus;
import com.genericdevice.platform.core.event.IEvent;
import com.genericdevice.platform.core.mvp.AbstractPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Xilent on 25.05.2014.
 */
public class FileDialogPresenter extends AbstractPresenter<FileDialogView> {
    private static final String START_PATH = "startPath";
    private static final String FILE_FROMAT = "fileFormat";
    private static final String DONE_EVENT = "doneEvent";

    public static final String FILE_PATH = "filePath";

    private String startPath;
    private String fileFormat;
    private String doneEvent;

    private List<Map<String, String>> fileList;

    private List<String> pathHistory = new ArrayList<String>();
    private String currentPath;

    @Override
    protected FileDialogView createView() {
        return new FileDialogView();
    }

    @Override
    public void init() {
        super.init();

        Bundle extras = activity.getIntent().getExtras();
        if (extras == null)
            return;

        startPath = extras.getString(START_PATH);
        fileFormat = extras.getString(FILE_FROMAT);
        doneEvent = extras.getString(DONE_EVENT);

        if (startPath == null)
            startPath = Environment.getExternalStorageDirectory().getAbsolutePath();
    }

    @Override
    protected void onLoad() {
        showFolderList(startPath);
    }

    private void showFolderList(String path) {
        currentPath = path;
        fileList = FileListHelper.getFolderList(path, fileFormat);
        view.loadFolderList(fileList);
    }

    public void call(Integer index) {
        Map<String, String> pathEntry = fileList.get(index);
        if (pathEntry.get(FileListHelper.TYPE_FIELD).equals(FileListHelper.FOLDER_TYPE)) {
            pathHistory.add(currentPath);
            showFolderList(pathEntry.get(FileListHelper.PATH_FIELD));
        } else {
            Map<String, String> params = new HashMap<String, String>();
            params.put(FILE_PATH, fileList.get(index).get(FileListHelper.PATH_FIELD));
            IEvent event = new DefaultEvent(params);
            EventBus.fireEvent(doneEvent, event);
            activity.finish();
        }
    }

    public boolean goBack() {
        if (pathHistory.size() > 0) {
            Integer index = pathHistory.size() - 1;
            String path = pathHistory.get(index);
            pathHistory.remove((int) index);

            showFolderList(path);

            return true;
        } else {
            return false;
        }
    }
}
